"""
Map categories, formats ... to their data.gov.be counterparts.
"""

__author__ = "Bart Hanssens <bart.hanssens@fedict.be>"

import argparse, os, logging
import json

logging.basicConfig(filename='map-datagov.log', level=logging.DEBUG)

def loadmapping(infile, name, mappings):
	"""Load mapping file and add mappings to dict."""
	logging.info('Load mapping file ' + str(infile))

	mapping = {} 

	with open(infile, 'r') as f:
		for line in f:
			(key, sep, values) = line.rstrip().partition('>')
			value = values.split(';')
			mapping[key] = value

	mappings[name] = mapping


def ckanmap(infile, outfile, mappings):
	"""Load JSON file and map categories... to data.gov.be entries"""
	logging.info('Parsing ' + str(infile))

	with open(infile, 'r') as f:
		data = json.load(f)

	mapkeys = mappings.keys()

	for key in data:
		if key in mapkeys:
			# Check if we need to map only one value or a list
			if isinstance(data[key], list):
				temp = set()
				for onekey in data[key]:
					if onekey in mappings[key]:
						temp |= set(mappings[key][onekey])
				data[key] = list(temp)
			else:
				data[key] = mappings[key].get(data[key])

	with open(outfile, 'w') as f:
		json.dump(data, f, indent=4, encoding='iso-8859-1')


def main():	
	parser = argparse.ArgumentParser(description='Map JSON files')
	parser.add_argument('--indir', help='Input directory', required=True)
	parser.add_argument('--mapdir', help='Mappings directory', required=True)
	parser.add_argument('--outdir', help='Output directory', required=True)
	
	args = parser.parse_args()
	
	mappings = {} 

	for f in os.listdir(args.mapdir):
		loadmapping(os.path.join(args.mapdir, f), f, mappings)

	for f in os.listdir(args.indir):
		ckanmap(os.path.join(args.indir, f), os.path.join(args.outdir, f), mappings)

if __name__ == "__main__":
	main()
